<template>
  <div class="container">
    <van-nav-bar @click-left="clickLeft" left-arrow>
      <template #title>
        <van-dropdown-menu>
          <van-dropdown-item v-model="value1" :options="subject_List" />
        </van-dropdown-menu>
      </template>
      <template #right>
        <van-icon ref name="apps-o" size="18" is-link @click="showDropDown" />
      </template>
    </van-nav-bar>
    <DropDown
      :show="showDrop"
      :list="dropList"
      @close="closeDrop"
      @change="changeDrop"
      class="DropDown"
      ref="DropDown"
    />
    <div class="phone-wrap">
      <van-tabs v-model="tabActive" swipeable>
        <van-tab title="章节练习">
          <ChapterExercise />
        </van-tab>
        <van-tab title="快速做题">
          <QuickExercise />
        </van-tab>
        <van-tab title="模拟真题">
          <AnalogExercise />
        </van-tab>
      </van-tabs>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import DropDown from "@/components/DropDown";
import ChapterExercise from "./ChapterExercise";
import QuickExercise from "./QuickExercise";
import AnalogExercise from "./AnalogExercise";
export default {
  components: {
    DropDown,
    ChapterExercise,
    QuickExercise,
    AnalogExercise
  },
  metaInfo: {
    title: "My Example App", // set a title
    meta: [
      {
        // set meta
        name: "keyWords",
        content: "My Example App"
      }
    ]
  },
  computed: {
    ...mapState({
      bank_id: state => state.question.bank_id,
      subject_id: state => state.question.subject_id,
      exerciseTab: state => state.question.exerciseTab,
      subject_List: state => {
        let arr = JSON.parse(state.question.subject_List);
        if (arr instanceof Array) {
          arr = arr.map(item => {
            item.text = item.subject_name;
            item.value = item.subject_id;
            return item;
          });
          return arr;
        }
      }
    })
  },
  data() {
    return {
      phoneBg: "#f3f3f3",
      dropList: [],
      showDrop: false,
      value1: 0,
      tabActive: 0
    };
  },
  created() {
    this.$nextTick(() => {
      this.tabActive = Number(this.exerciseTab);
      this.value1 = Number(this.subject_id);
    });
    this.getDrop();
  },
  watch: {
    value1(val, old) {
      if (val != old) {
        this.$store.commit("subject_id", val);
      }
    },
    tabActive(val, old) {
      if (val != old) {
        this.$store.commit("exerciseTab", val);
      }
    }
  },
  methods: {
    getDrop() {
      let param = {
        bank_id: this.bank_id,
        subject_id: this.subject_id
      };
      this.$axios.post("/bank/getCollectErrorExamCount", param).then(res => {
        if (res.code == 200) {
          let data = res.data;
          this.dropList = [
            {
              name: "我的收藏(" + data.collect_count + ")",
              path: "/question/collectionQuestion"
            },
            {
              name: "错题本(" + data.error_count + ")",
              path: "/question/errorQuestion"
            },
            {
              name: "做题记录(" + data.exam_count + ")",
              path: "/question/recordQuestion"
            }
          ];
        }
      });
    },
    changeDrop(item) {
      this.showDrop = false;
      this.$router.push(item.path);
    },
    closeDrop() {
      this.showDrop = false;
    },
    showDropDown(e) {
      this.showDrop = true;
      setTimeout(() => {
        this.$refs.DropDown.$children[0].$el.style.top = e.clientY + "px";
        this.$refs.DropDown.$children[0].$el.style.left =
          e.clientX - 180 + "px";
      }, 10);
      // debugger;
    },
    clickLeft() {
      this.$router.push("/question");
    }
  }
};
</script> 

<style lang="less" scoped>
@import "../question.less";
/deep/ .van-nav-bar {
  z-index: 99;
}

/deep/ .DropDown .van-popup--top {
  width: 200px;
}
.phone-wrap {
  background-color: #f3f3f3;
}
</style>
